package com.cheer.dto;

import com.cheer.entity.Organization;
import com.cheer.enums.RoleEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Data
@Accessors(chain = true)
public class OrganizationAddDTO {

    @NotNull(message = "上级机构不能为空")
    private Long parentOrgId;

    @NotBlank(message = "机构名称不能为空")
    @Length(max = 50, message = "机构名称不能超过50个字符")
    private String orgName;

    public Organization build(Organization parentOrganization) {
        Organization organization = new Organization();
        organization.setOrgName(orgName);
        organization.setParentOrgId(parentOrgId);
        organization.setOrgType(parentOrganization.getOrgType());
        if (parentOrganization.getCategory()) {
            organization.setOrgRole(RoleEnum.SECOND_ADMIN.getCode());
        } else {
            organization.setOrgRole(RoleEnum.LOWER_ADMIN.getCode());
        }
        organization.setOrgPath(parentOrganization.getOrgPath());
        organization.setLevel(parentOrganization.getLevel() + 1);
        organization.setCategory(false);
        return organization;
    }

}
